package com.work.formaldehyde.activityadapter;

import android.app.Activity;
import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.work.formaldehyde.R;
import com.work.formaldehyde.activity.AddressActivity;
import com.work.formaldehyde.activity.GetAddressActivity;
import com.work.formaldehyde.model.GetAddressModel;

import java.util.ArrayList;

public class AddressManageAdapter extends BaseAdapter {
    ArrayList<GetAddressModel.data> list;
    Context context;
    Activity activity;

    public AddressManageAdapter(ArrayList<GetAddressModel.data> list, Context context, Activity activity) {
        // TODO Auto-generated constructor stub
        this.list = list;
        this.context = context;
        this.activity = activity;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list != null ? list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list != null ? list.get(position) : 0;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        convertView = LayoutInflater.from(context).inflate(R.layout.address_list_item, null);
        init i = new init();
        i.title = convertView.findViewById(R.id.title);
        i.phone = convertView.findViewById(R.id.phone);
        i.address_msg = convertView.findViewById(R.id.address_msg);
        i.delf_texts = convertView.findViewById(R.id.delf_text);
        i.tianqi_qingkuang = convertView.findViewById(R.id.tianqi_qingkuang);
        i.title.setText(list.get(position).getConsignee_name());
        i.phone.setText(list.get(position).getConsignee_phone());
        i.address_msg.setText(list.get(position).getConsignee_city() + list.get(position).getConsignee_addr());
        switch (list.get(position).getIsdefault()) {
            case "1":
                i.delf_texts.setVisibility(View.VISIBLE);
                break;

            case "0":
                i.delf_texts.setVisibility(View.GONE);
                break;
        }
        i.tianqi_qingkuang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent toaddress = new Intent(context, AddressActivity.class);
                toaddress.putExtra("opentype", "1");
                toaddress.putExtra("name", list.get(position).getConsignee_name());
                toaddress.putExtra("addr", list.get(position).getConsignee_addr());
                toaddress.putExtra("phone", list.get(position).getConsignee_phone());
                toaddress.putExtra("default", list.get(position).getIsdefault());
                toaddress.putExtra("addrid", list.get(position).getId());
                context.startActivity(toaddress, ActivityOptions.makeSceneTransitionAnimation(activity).toBundle());
            }
        });
        convertView.setTag(i);
        return convertView;
    }

    public static class init {
        //首页list的子控件
        TextView title, phone, address_msg;
        LinearLayout delf_texts;
        ImageView tianqi_qingkuang;
    }
}

